package com.fzkjjt.nacos.controller;

import com.alibaba.fastjson.JSON;
import com.fzkjjt.nacos.api.DiscoveryApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: optms-nacos
 * @description:
 * @author: FangZhen
 * @create: 2020-08-12 15:47
 **/
@Slf4j
@RestController
public class DiscoveryController implements DiscoveryApi {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String get(String serviceName) {
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
        return JSON.toJSONString(instances);
//        String targetUrl = instances.get(0).getUri() + "/discovery/echo?name=123";
//        String response = restTemplate.getForObject(targetUrl, String.class);
//        return response;
    }

    @Override
    public String echo(String name) {
        return "欢迎你:" + name;
    }
}
